//
//  YMNetDataUtil.m
//  MyProject
//
//  Created by Pei on 16/6/19.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//
#import "YMNetDataUtil.h"
#import <CommonCrypto/CommonDigest.h>
#import "YMUtil.h"
#import "YMNetLib.h"
#import "YMNetDataUtil.h"

@implementation YMNetDataUtil

SYNTHESIZE_SINGLETON_FOR_CLASS(YMNetDataUtil);

+(NSDictionary*)formatePostDataWithSysHead:(id)sysHead withAppHead:(id)appHead withBody:(id)body{
    if (body == [NSNull null]) {
        body = [NSDictionary dictionaryWithObjectsAndKeys:nil];
    }
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:sysHead,@"SYSHEAD",appHead,@"APPHEAD",body,@"BODY",nil];
    return dic;
}

+(id)getSysHead:(NSDictionary*)response{
    return [response objectForKey:@"SYSHEAD"];
}
+(id)getSysCode:(NSDictionary*)sysHead{
    return [sysHead objectForKey:@"RSP_CODE"];
}
+(id)getSysSessionID:(NSDictionary*)sysHead{
    return [sysHead objectForKey:@"SESSION_ID"];
}



+(id)getSysMsg:(NSDictionary*)sysHead{
    return [sysHead objectForKey:@"msg"];
}

+(id)getSuccessState:(NSDictionary*)sysHead{
    return [sysHead objectForKey:@"Success"];
}

+(BOOL)isNoDataState:(NSDictionary*)sysHead{
    return [[sysHead objectForKey:@"error"] intValue] == 4230 ? YES : NO;
}

+(BOOL)isLatestVersion:(NSDictionary*)sysHead{
    return [[sysHead objectForKey:@"error"] intValue] == 4210 ? YES : NO;
}

+(id)getBody:(NSDictionary*)response{
    return [response objectForKey:@"data"];
}

+(id)getState:(NSDictionary*)response
{
   return [response objectForKey:@"state"]; 
}

+(NSString*)formateSysHeadWithAction:(NSString*)action
{
    //&versionType=individual
    //&platform=ios
    //&versionCode=100
    NSString *tempStr = [NSString stringWithFormat:@"%@?versionType=personal&platform=ios&versionCode=%@&",action,[YMUtil getVersion],nil];
    return tempStr;
}

+(NSString*)formateSysHeadWithAction
{
    //&versionType=individual
    //&platform=ios
    //&versionCode=100
    NSString *tempStr = [NSString stringWithFormat:@"&versionType=personal&platform=ios&versionCode=%@",[YMUtil getVersion],nil];
    return tempStr;
}

+(NSDictionary*)formateDicSysHeadWithAction
{
    NSDictionary *tempDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"personal",@"versionType",@"ios",@"platform",[YMUtil getVersion],@"versionCode",nil];
    return tempDic;
}


+(NSString*)md5:(NSString*)str{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (int)strlen(cStr), result);
    
    NSMutableString *Mstr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    for (int i=0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [Mstr appendFormat:@"%02X",result[i]];
    }
    NSString *upperStr = [Mstr uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSLog(@"upperStr: %@", upperStr);
    return Mstr;
}


@end
